package com.chen.authserver.v1.repository;

import com.chen.authserver.v1.DO.FinalAuthUser;
import com.chen.authserver.v1.VO.AuthUserVO;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Repository
public interface UserMysqlRepository {

    @Select("select * from final_auth_user")
    List<FinalAuthUser> findAll();

    @Select("select * from final_auth_user where accountname = #{account}")
    FinalAuthUser find(String account);

    @Select("select id, accountname, nickname, tsex, email, phonenumber, birthday from final_auth_user where accountname = #{account}")
    AuthUserVO find2(String account);

    @Select("select pwd from final_auth_user where accountname = #{account}")
    String findPwd(String account);

    @Select("select * from final_auth_user where phonenumber = #{phonenumber}")
    FinalAuthUser findByPhoneNum(String phonenumber);

    @Select("select id, accountname, nickname, tsex, email, phonenumber, birthday from final_auth_user where phonenumber = #{phonenumber}")
    AuthUserVO findByPhoneNum2(String phonenumber);

    @Transactional(rollbackFor = Exception.class)
    @Insert("insert into final_auth_user (accountname, nickname, sex, tsex,pwd, phonenumber, email, idcard, birthday, createtime) " +
            "values (#{accountName}, #{nickName}, #{sex}, #{tsex}, #{pwd}, #{phoneNumber}, #{email}, #{idCard}, #{birthDay}, #{createTime})")
    void save(FinalAuthUser finalAuthUser);

    @Select("select id from final_auth_user where accountname = #{account}")
    Integer findId(String account);

    @Select("select id from final_auth_user where phonenumber = #{phonenumber}")
    Integer findIdByPhoneNum(String phonenumber);

    @Select("select id from final_auth_user where email = #{email}")
    Integer findIdByEmail(String email);

    @Select("select id from final_auth_user where idcard = #{idcard}")
    Integer findIdByIdCard(String idcard);
}
